package com.acme.bankapp.service.bank;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.exceptions.ClientExistException;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: Alfeya
 * Date: 23.11.14
 * Time: 15:47
 * To change this template use File | Settings | File Templates.
 */
public class BankDataLoaderService {
    public static void saveBank(Bank bank, String fileName) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
        for ( Client c : bank.clients) {
            if ( c != null ) {
                for ( Account a : c.getAccounts()) {
                    if ( a != null ) {
                        StringBuffer sb = new StringBuffer();
                        sb.append("accounttype=");
                        if ( a instanceof CheckingAccount )
                            sb.append("c;");
                        else
                            sb.append("s;");

                        sb.append("balance=" + a.getBalance() + ";");
                        if( a instanceof CheckingAccount )
                            sb.append("overdraft=" + ((CheckingAccount)a).getOverdraft() + ";");

                        sb.append("name=" + c.getName() + ";");
                        sb.append("gender=" + ( c.getGender() == Client.Gender.MALE ? "m" : "f" ) + ";");
                        sb.append("\n");

                        oos.writeObject((String) sb.toString());
                    }
                }
            }
        }
        oos.writeObject("EOF");
        oos.close();
    }

    public static Bank loadBank(String fileName) {
        Bank bank = new Bank(10, new ClientRegistrationListener[2]);
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
            String line;
            String clientName = null;
            Client.Gender clientGender = null;
            char accountType = 0;
            double accountBalance = 0;
            double accountOverdraft = 0;
            while ( !"EOF".equals(line = (String)ois.readObject())) {
                Scanner scanner = new Scanner(line).useDelimiter("\\s*;\\s*");
                while( scanner.hasNext()) {
                    String innerLine = scanner.next();
                    Scanner innerScanner = new Scanner(innerLine).useDelimiter("\\s*=\\s*");
                    String field = innerScanner.next();
                    String value = innerScanner.next();
                    if ("accounttype".equals(field)) {
                        accountType = value.charAt(0);
                    }
                    if ("balance".equals(field)) {
                        accountBalance = Double.parseDouble(value);
                    }
                    if ("overdraft".equals(field)) {
                        accountOverdraft = Double.parseDouble(value);
                    }
                    if ("name".equals(field)) {
                        clientName = value;
                    }
                    if ("gender".equals(field)) {
                        if ("m".equals(value)) {
                            clientGender = Client.Gender.MALE;
                        } else {
                            clientGender = Client.Gender.FEMALE;
                        }
                    }
                }
                Account account = null;
                if ('c' == accountType) {
                    account = new CheckingAccount(accountBalance, accountOverdraft);
                } else {
                    account = new SavingAccount(accountBalance);
                }
                boolean clientExists = false;
                for (Client client : bank.clients) {
                    if (client != null) {
                        if (client.getName().equals(clientName)) {
                            client.addAccount(account);
                            clientExists = true;
                        }
                    }
                }
                if (clientExists == false) {
                    Client client = new Client(clientName, clientGender);
                    client.addAccount(account);
                    bank.addClient(client);
                }
            }
            ois.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClientExistException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return bank;
    }
}